//
//  GFMoreMessageViewController.m
//  GoodFood
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFMoreMessageViewController.h"

@interface GFMoreMessageViewController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation GFMoreMessageViewController

- (void)viewWillAppear:(BOOL)animated {
    
    UIView *view = (UIView *)[self.navigationController.view viewWithTag:10001];
    view.backgroundColor = [UIColor colorWithRed:190 / 255.0 green:231 / 255.0 blue:233 / 255.0 alpha:1];
    
    
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:209 / 255.0 green:186 /255.0 blue:116 / 255.0 alpha:1];
    
    self.title = @"更多功能";
    
    UIColor *color = [UIColor colorWithRed:209 / 255.0 green:186 / 255.0 blue:116 / 255.0 alpha:1];
    
    NSDictionary *dic = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];/* 在NSFontAttributeName中寻找*/
    
    self.navigationController.navigationBar.titleTextAttributes = dic;
    
    
}

#pragma mark _  创建tableView
- (void)creatTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    /* 重用池 */
    [self.tableView registerClass:[GFMoreCell class] forCellReuseIdentifier:@"reuse"];
    
    [self.view addSubview:self.tableView];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
    
}

#pragma mark _ tableView DataSource 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0 ) {
        return 3;
    } else {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        NSArray *array1 = @[@"我的收藏", @"清除缓存", @"关于我们"];
        NSArray *array2 = @[@"shoucang",@"", @"denglu"];
        
        GFMoreCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.textLabel.text = [array1 objectAtIndex:indexPath.row];
        cell.imageView.image = [UIImage imageNamed:[array2 objectAtIndex:indexPath.row]];
        
        return cell;
    } else {
        
        GFMoreCell *cell = [[GFMoreCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"reuse"];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.textLabel.text = @"常见食物的GI值";
        cell.detailTextLabel.text = @"糖尿病患者的升糖指数表";
        cell.detailTextLabel.textColor = [UIColor lightGrayColor];
        
        return cell;
        
    }
    
}

#pragma mark _ tableView Delegate 协议方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            
            GFCollectViewController *collectVC = [[GFCollectViewController alloc] init];
            
            [self.navigationController pushViewController:collectVC animated:YES];
            

            
        } else if (indexPath.row == 1) {
            
            NSFileManager *manager = [NSFileManager defaultManager];
            NSArray *path =NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
            
            NSString *pathString =path.lastObject;
            
            NSString *pathLast =@"/Caches/com.hackemist.SDWebImageCache.default/";
            
            
            //创建字符串文件存储路径
            NSString *PathName =[pathString stringByAppendingString:pathLast];
            float sizeAll = 0;
            if([manager fileExistsAtPath:PathName]){
                long long size=[manager attributesOfItemAtPath:PathName error:nil].fileSize;
                sizeAll = size;
                
            }
            [manager removeItemAtPath:PathName error:nil];
            NSString *clearMessage = sizeAll >= 1024 * 1024 ? [NSString stringWithFormat:@"清理缓存(%.2fM)", sizeAll / 1024] : [NSString stringWithFormat:@"清理缓存(%.2fK)", sizeAll];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"清除缓存" message:clearMessage preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [[SDImageCache sharedImageCache]clearDisk];
                
                //清除内存缓存
                [[[SDWebImageManager sharedManager] imageCache] clearMemory];
                
                //清除系统缓存
                [[NSURLCache sharedURLCache] removeAllCachedResponses];
                
            }];
            [alert addAction:alertAction];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            
        } else {
            
            GFAboutUsViewController *aboutUsVC = [[GFAboutUsViewController alloc] init];
            
            [self.navigationController pushViewController:aboutUsVC animated:YES];
        }
    } else {
        
        GFGIViewController *GIVC = [[GFGIViewController alloc] init];
        
        [self.navigationController pushViewController:GIVC animated:YES];
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth / 375.0 * 375, ScreenWidth / 375.0 * 44)];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth / 375.0 * 20, ScreenWidth / 375.0 * 10, ScreenWidth / 375.0 * 100, ScreenWidth / 375.0 * 24)];
    label.text = @"功能";
    label.font = [UIFont systemFontOfSize:17];
    [view addSubview:label];
    
    if (section == 1) {
        label.text = @"其他";
    }
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 44;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.waitView removeFromSuperview];
    [self creatTableView];
    // Do any additional setup after loading the view.
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"houtui1"] style:UIBarButtonItemStylePlain target:self action:@selector(backToLastVC:)];
    self.navigationItem.leftBarButtonItem = backItem;

}

- (void)backToLastVC:(UIBarButtonItem *)item {
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    YRSideViewController *con = (YRSideViewController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    
    [con showLeftViewController:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
